<?php
namespace App\Http\Controllers\ApiApp;

use App\DBs\MemberDB;
use Illuminate\Http\Request;

class MemberController extends BaseController
{
    //用户

    protected $db;

    public function __construct()
    {
        parent::__construct();
        $this->db = new MemberDB();
    }

    /**
     * 用户列表
     */
    public function getIndex(Request $request)
    {
        $resDb = $this->db->getIndex();
        if ($resDb['code']!=0) {
            return $this->toAppJson(-3);
        }
        $items = $resDb['data'];
        return $this->toAppJson(0,$items);
    }

    /**
     * 通过手机号码，获取用户
     */
    public function getOneByPhone(Request $request)
    {
        $phone = $request->post('phone','');
        $resDb = $this->db->getOneByPhone($phone);
        if (!$resDb) {
            return $this->toAppJson(-3);
        }
        return $this->toAppJson(0,$resDb);
    }

    /**
     * 通过微信号，获取用户
     */
    public function getOneByWx(Request $request)
    {
        $wx = $request->post('wx','');
        $resDb = $this->db->getOneByWx($wx);
        if (!$resDb) {
            return $this->toAppJson(-3);
        }
        return $this->toAppJson(0,$resDb);
    }

    /**
     * 新增用户
     */
    public function setAdd(Request $request)
    {
        $phone = $request->post('phone','');
        $wx = $request->post('wx','');
        $xiaoqu = $request->post('xiaoqu','');
        if (!$wx) {
            return $this->toAppJson(-2);
        }
        if (strlen($phone)<11) {
            return array('code'=>'-4','msg'=>'手机号码错误');
        }
        $resDb = $this->db->setAdd($phone,$wx,$xiaoqu);
        if (!$resDb) {
            return $this->toAppJson(-500);
        }
        return $this->toAppJson(0);
    }
}